package com.hy.service.impl;

import com.hy.bean.ReceiveApply;
import com.hy.dao.ReceiveApplyDao;
import com.hy.dao.impl.ReceiveApplyDaoImpl;
import com.hy.service.ReceiveApplyService;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

public class ReceiveApplyServiceImpl implements ReceiveApplyService {
    private ReceiveApplyDao receiveApplyDao = new ReceiveApplyDaoImpl();

    @Override
    public Map<String, Object> insert(ReceiveApply receiveApply) throws Exception {
        Boolean flag = receiveApplyDao.insert(receiveApply);
        if (flag){
            Map<String, Object> map = receiveApplyDao.selectNew();
            return map;
        }
        return null;
    }
    @Override
    public List<Map<String, Object>> select(String deptName, String username) throws Exception {
        if ( ("null".equalsIgnoreCase(deptName))){
            deptName = "";
        }
        if("null".equalsIgnoreCase(username)){
            username = "";
        }

        Connection conn = DBManager.getConnection();
        List<Map<String, Object>> mapList = null;

        if ((StringUtils.isBlank(deptName) && StringUtils.isBlank(username)))
            mapList = receiveApplyDao.select(conn);
        else if (StringUtils.isBlank(deptName) && !StringUtils.isBlank(username))
            mapList = receiveApplyDao.select(conn,deptName);
        else if (!StringUtils.isBlank(deptName) && StringUtils.isBlank(username))
            mapList = receiveApplyDao.select(conn,username,-1);
        else
            mapList = receiveApplyDao.select(conn,deptName,username);

        DbUtils.close(conn);
        return mapList;
    }

    @Override
    public Boolean deleteApply(Integer id) throws Exception {
        List<Map<String, Object>> maps = receiveApplyDao.selectApplyDetailByApplyId(id);
        if ( maps != null && maps.size()>=1) {
            return false;
        }
        Boolean flag = receiveApplyDao.deleteById(id);
        return flag;
    }

    @Override
    public Boolean update(ReceiveApply receiveApply) throws Exception {
        return receiveApplyDao.update(receiveApply);
    }

    @Override
    public Boolean updateDjstatus(Integer id) throws Exception {
        return receiveApplyDao.updateDjstatus(id);
    }
}
